package ynu.sxp.demoapp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.sxp.demoapp.entity.Orders;
import ynu.sxp.demoapp.entity.OrderDetailet;
import ynu.sxp.demoapp.repository.OrdersRepository;
import ynu.sxp.demoapp.repository.OrderDetailetRepository;
import ynu.sxp.demoapp.service.OrderService;

import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    private final OrdersRepository ordersRepository;
    private final OrderDetailetRepository orderDetailetRepository;

    @Autowired
    public OrderServiceImpl(OrdersRepository ordersRepository, OrderDetailetRepository orderDetailetRepository) {
        this.ordersRepository = ordersRepository;
        this.orderDetailetRepository = orderDetailetRepository;
    }

    @Override
    @Transactional
    public Orders createOrder(Orders order) {
        return ordersRepository.save(order);
    }

    @Override
    public Orders getOrderById(UUID orderId) {
        return ordersRepository.findById(orderId).orElse(null);
    }

    @Override
    public List<Orders> getOrdersByUserId(String userId) {
        return ordersRepository.findByUserId(userId);
    }

    @Override
    @Transactional
    public void updateOrder(Orders order) {
        ordersRepository.save(order);
    }

    @Override
    @Transactional
    public void deleteOrder(UUID orderId) {
        ordersRepository.deleteById(orderId);
    }

    @Override
    public List<OrderDetailet> getOrderDetailsByOrderId(UUID orderId) { // 修改为 UUID 类型
        return orderDetailetRepository.findByOrderId(orderId);
    }

    @Override
    public List<Orders> getAllOrders() {
        return ordersRepository.findAll();
    }

    @Override
    public List<Orders> getOrdersByStatus(Integer status) {
        return ordersRepository.findByOrderState(status);
    }

    @Override
    @Transactional
    public void deleteOrders(List<UUID> orderIds) {
        ordersRepository.deleteAllById(orderIds);
    }
}